package com.bafa.front;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bafa.dbcache.LeagueCache;
import com.bafa.exceptions.ErrorException;
import com.bafa.front.presentationbeans.SeasonBean;
import com.bafa.front.presentationbeans.SeasonListBean;
import com.bafa.front.presentationbeans.SiteBean;
import com.bafa.front.utils.FrontUtils;
import com.bafa.jdbc.DBAccess;
import com.bafa.objects.Message;
import com.bafa.objects.PageBean;
import com.bafa.objects.User;
import com.bafa.objects.season.Season;
import com.bafa.services.RetrieveServices;
;

public class DaddyServlet extends HttpServlet {

	protected String url = "";
	protected Message message;
	protected PageBean pageBean;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		DBAccess db = new DBAccess();
		
		
		Season season = RetrieveServices.getCurrentSeason();
		SeasonBean seasonBean = null;
		if(season==null){
			seasonBean = new SeasonBean();
		}
		else{
			seasonBean = new SeasonBean(season);
		}
		request.setAttribute("CURRENTSEASON",seasonBean);
		try{
			pageBean = null;
			url = "";
			message = new Message();
			HttpSession session = request.getSession();
			
				
			if(request.getParameter("SeasonId")!=null&&!request.getParameter("SeasonId").equals(""))
				session.setAttribute("THISSEASON",new SeasonBean(RetrieveServices.getSeason(request.getParameter("SeasonId"))));
			else if(session.getAttribute("THISSEASON")==null)
				session.setAttribute("THISSEASON",seasonBean);
			doEvent(request,response);
			if(message!=null)
				request.setAttribute("MESSAGE",message);
			if(session.getAttribute("ALLSEASONS")==null){
				session.setAttribute("ALLSEASONS", new SeasonListBean(RetrieveServices.getLeagueSeasons()));
			}
			request.setAttribute("SITENAME",db.retrieveReference("sitename"));
			if(session.getAttribute("SITEBEAN")==null){
				SiteBean siteBean = new SiteBean();
				siteBean.setStylesheet(db.retrieveReference("style"));
				siteBean.setDivbackground(db.retrieveReference("divbackground"));
				session.setAttribute("SITEBEAN",siteBean);
			}
			
			if(getPageBean()!=null){
				request.setAttribute("PAGEBEAN",getPageBean());
				request.getRequestDispatcher("template.jsp").forward(request,response);
			}else if(!getURL().equals(""))request.getRequestDispatcher(getURL()).forward(request,response);
		}catch(ErrorException e){
                e.printStackTrace();
			request.setAttribute("ERROR",e);
			request.getRequestDispatcher("error.jsp").forward(request,response);
		}catch(Exception f){
			f.printStackTrace();
			ErrorException error = new ErrorException();
			error.addErrorMessage("Unknown Error has occurred. Please inform the administrator at bafa.bafa@ba.com: Time and date of error "+Calendar.getInstance().get(Calendar.DATE)+" "+(Calendar.getInstance().get(Calendar.MONTH)+1)+" "+Calendar.getInstance().get(Calendar.YEAR)+" at "+(Calendar.getInstance().get(Calendar.HOUR)+1)+":"+Calendar.getInstance().get(Calendar.MINUTE) +" : ");
			
			
			for(int i=0;i<f.getStackTrace().length;i++){
				error.addErrorMessage(f.getStackTrace()[i].getClassName()+" ("+f.getStackTrace()[i].getLineNumber()+")");
			}
			
			request.setAttribute("ERROR",error);
			request.getRequestDispatcher("error.jsp").forward(request,response);
		}
		
	}
	
	public String getURL(){
		return url;
	}
	
	public PageBean getPageBean(){
		return pageBean;
	}
	public void doEvent(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, ErrorException {
		
	}
}
